package com.t.blog.service.impl;

import com.mysql.cj.xdevapi.UpdateResult;
import com.t.blog.mongodb.dao.base.Impl.BaseDao;
import com.t.blog.mongodb.vo.ArticleVo;
import com.t.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.t.pro.dao.ArticleDao;
import org.t.pro.pojo.AdminQueryArticlePojo;
import org.t.pro.pojo.AdminSaveArticlePojo;
import org.t.pro.vo.Article;

import java.awt.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author a0000
* @description 针对表【article】的数据库操作Service实现
* @createDate 2024-02-05 23:08:07
*/
@Service
public class ArticleServiceImpl extends BaseDao<ArticleVo> implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    @Override
    public List<AdminQueryArticlePojo> getArticleList() {
        List<AdminQueryArticlePojo> articleList = articleDao.getArticleList();
        //构造树形菜单
        //TODO 统一成公共工具方法
        Map<Integer,List<AdminQueryArticlePojo>> groups = articleList.stream().collect(Collectors.groupingBy(AdminQueryArticlePojo::getParentId));
        articleList.forEach(article -> {
            Integer id = article.getId();
            List<AdminQueryArticlePojo> childs = groups.get(id);
            article.setChildrenArticle(childs);
        });
        return  articleList.stream().filter(a -> a.getParentId() == 0).collect(Collectors.toList());
    }

    @Override
    public AdminQueryArticlePojo getArticleContentById(AdminQueryArticlePojo pojo) {
        AdminQueryArticlePojo articlePojo = articleDao.getArticleById(pojo.getId());
        ArticleVo article = findOne(String.valueOf(pojo.getId()));
        if (article != null){
            articlePojo.setContent(article.getContent());
        }
        return articlePojo;
    }

    @Override
    public void saveArticle(AdminSaveArticlePojo pojo) {
        // 保存 文章 到 mysql
        try{
            articleDao.saveArticle(pojo);
        }catch (Exception e){
            e.printStackTrace();
        }
        //根据 保存结果 保存 内容 到 mongodb
        ArticleVo articleVo = new ArticleVo();
        articleVo.set_id(String.valueOf(pojo.getId()));
        articleVo.setContent(pojo.getContent());
        this.insert(articleVo);
    }

    @Override
    public void motifyArticle(AdminSaveArticlePojo pojo) {
        // 更新 文章 到 mysql
        try{
            articleDao.saveArticle(pojo);
        }catch (Exception e){
            e.printStackTrace();
        }
        //根据 保存结果 更新 内容 到 mongodb
        Update update = new Update();
        update.addToSet("content",pojo.getContent());
        Query query = new Query();
        query.addCriteria(new Criteria("_id").is(String.valueOf(pojo.getId())));
        this.update(query,update);
    }


}




